package com.twotiger.and.activity.current;

import java.util.ArrayList;
import java.util.HashMap;

import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.twotiger.and.Constants;
import com.twotiger.and.TwoTigerApp;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.activity.base.BaseHandler;
import com.twotiger.and.activity.base.SubTitle;
import com.twotiger.and.adapter.CurrentProfitRecordAdapter;
import com.twotiger.and.adapter.OperationRecordAdapter;
import com.twotiger.and.bean.AssertMatch;
import com.twotiger.and.bean.Basebean;
import com.twotiger.and.bean.DataList;
import com.twotiger.and.bean.ProfitRecord;
import com.twotiger.and.util.ArithUtils;
import com.twotiger.and.util.TimeUtils;
import com.view.pulltorefresh.MyListView;
import com.view.pulltorefresh.PullToRefreshBase;
import com.view.pulltorefresh.PullToRefreshBase.OnLastItemVisibleListener;
import com.view.pulltorefresh.PullToRefreshMyListView;

/**
 * 活期收益记录
 * @Description: 
 * intent需要携带 PROJECT_Id or PROJECT_DETAIL
 * @Author douxy
 * @Date 2015年8月12日 下午2:43:22
 */
public class CurrentProfitRecordPage extends BaseActivity implements OnClickListener {

    private static final int GETINTERESTSUCCSE = 0;
    private static final int GETINTERESTFAIL = 1;

    private static HashMap<String, String> params;
    private int pNum = 1;
    private SubTitle subTitle;
    private RelativeLayout no_content_rl;
    private TextView tv_profit_month, tv_profit_week;
    private PullToRefreshMyListView lv_profit;
    private ImageView iv_no_content;
    private CurrentProfitRecordAdapter mCurrentProfitRecordAdapter;
    private View current_profit_record_header;

    @Override
    protected View loadViewLayout(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.current_profit_record_list_layout, null);
        current_profit_record_header = inflater.inflate(R.layout.current_profit_record_header, null);
        return view;
    }

    @Override
    protected void findViewById(View view) {
        subTitle = new SubTitle(view) {
            @Override
            protected void clickTitle(View v) {
            }

            @Override
            protected void clickRightBtn(View v) {
            }

            @Override
            protected void cickLeftBtn(View v) {
                onBackPress();
            }
        };
        subTitle.setTitle("收益记录");
        subTitle.hideBtns();
        subTitle.title_left_bt.setVisibility(View.VISIBLE);

        lv_profit = (PullToRefreshMyListView) view.findViewById(R.id.lv_profit);
        no_content_rl = (RelativeLayout) view.findViewById(R.id.no_content_rl);
        iv_no_content = (ImageView) view.findViewById(R.id.iv_no_content);
        
        //头布局id
        tv_profit_week = (TextView) current_profit_record_header.findViewById(R.id.tv_profit_week);
        tv_profit_month = (TextView) current_profit_record_header.findViewById(R.id.tv_profit_month);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.iv_no_content:
            refreshlist();
            break;
        default:
            break;

        }
    }

    @Override
    protected void setListener() {

        handler = new BaseHandler(this) {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case GETINTERESTSUCCSE:
                    Basebean result = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if (result.isOk()) {
                        lv_profit.getLoadingLayoutProxy().setLastUpdatedLabel(
                            "最后更新：今天 " + TimeUtils.getFormatDate(" HH:mm"));
                        DataList dataList = JSON.parseObject(result.data, DataList.class);
                        fillData(dataList);
                        ArrayList<ProfitRecord> profitRecordList = (ArrayList<ProfitRecord>) JSON
                            .parseArray(dataList.getList(), ProfitRecord.class);
                        if (!profitRecordList.isEmpty()) {
                            if(profitRecordList.size() < Integer.parseInt(Constants.pageSize)){
                                lv_profit.changeMyFooterViewContent(false);
                            }else{
                                lv_profit.changeMyFooterViewContent(true);
                            }
                            no_content_rl.setVisibility(View.GONE);
                            if (pNum == 1) {
                                mCurrentProfitRecordAdapter.resetData(profitRecordList);
                            } else {
                                mCurrentProfitRecordAdapter.addData(profitRecordList);
                            }
                        } else {
                            //请求数据是空的
                            if (pNum == 1) {
                                no_content_rl.setVisibility(View.VISIBLE);
                                iv_no_content.setImageResource(R.drawable.no_content);
                            } else {
                                lv_profit.changeMyFooterViewContent(false);
                                mCurrentProfitRecordAdapter.notifyDataSetChanged();
                            }
                        }
                    } else {
                        showToast(result.codeDesc);
                    }

                    lv_profit.onRefreshComplete();
                    break;
                case GETINTERESTFAIL:

                    lv_profit.onRefreshComplete();
                    break;
                default:
                    break;
                }

                super.handleMessage(msg);
            }
        };
        lv_profit.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

            @Override
            public void onLastItemVisible() {
                String footerContent = ((TextView)lv_profit.getChildviewOfFooterview()).getText().toString();
                if("已无更多项目".equals(footerContent)){
                    return;
                }
                /**
                 * 加载更多数据
                 */
                 pNum++;
                 refreshlist();
            }
        });
        lv_profit.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<MyListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<MyListView> refreshView) {
                /**
                 * 刷新数据
                 */
                pNum = 1;
                refreshlist();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<MyListView> refreshView) {
                /**
                * 加载更多数据
                */
                pNum++;
                refreshlist();
            }
        });
        iv_no_content.setOnClickListener(this);
        no_content_rl.setOnClickListener(this);
    }

    protected void fillData(DataList dataList) {
        tv_profit_month.setText(ArithUtils.coverMoneyComma(dataList.getMonthInterest() + ""));
        tv_profit_week.setText(ArithUtils.coverMoneyComma(dataList.getWeekInterest() + ""));
    }

    @Override
    protected void processLogic() {
        params = getParm();
        ArrayList<ProfitRecord> dataList = new ArrayList<ProfitRecord>();
        mCurrentProfitRecordAdapter = new CurrentProfitRecordAdapter(CurrentProfitRecordPage.this,
            dataList);
        lv_profit.getRefreshableView().addHeaderView(current_profit_record_header);
        lv_profit.setAdapter(mCurrentProfitRecordAdapter);
        refreshlist();
    }

    private void refreshlist() {
        //请求之前先看是否开通活期,如果没有则不用请求，直接显示无记录布局
        if (!TwoTigerApp.getInstance().getUser().hasOpenProjectXAct) {
            no_content_rl.setVisibility(View.VISIBLE);
            iv_no_content.setImageResource(R.drawable.no_content);
            return;
        }
        
        params.clear();
        params.put("token", getToken());
        params.put("pageNum", String.valueOf(pNum));
        params.put("timestamp", TimeUtils.getTimeStamp());
        params.put("pageSize", Constants.pageSize);
        getDate(params, Constants.INTERESTXQUERY, handler, GETINTERESTSUCCSE,
            GETINTERESTFAIL, true, true, false);
    }

    @Override
    protected void onBackPress() {
        finish();
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

}
